package com.roast.pages;

import java.util.ArrayList;
import java.util.List;
import com.roast.domain.*;
import com.roast.services.my.MyServicesFacade;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionAttribute;
import org.apache.tapestry5.ioc.annotations.Inject;

public class UserComments {

    @Inject
    MyServicesFacade myServices;

    @SessionAttribute
    boolean loginSA;

    @SessionAttribute
    @Property
    UserData userSA;

    @Property
    String commentText;

    @Property
    String systemMessage = "     ";

    @Property
    Comment comment;

    private boolean commentButton;

    @Persist
    private Address address;

    @Persist
    private long starID;

    void onSelectedFromCommentButton() {
        commentButton = true;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Address getAddress() {
        return address;
    }

    public void setStarID(long starID) {
        this.starID = starID;
    }

    public List<Comment> getCommentList() {
        List<Comment> commentList = address.getCommentList();
        if (!commentList.isEmpty()) {
            return commentList;
        }
        return null;
    }

    public boolean isNoCommentsTest() {
        return getCommentList() == null;
    }

    public boolean isCommentTest() {
        long userLoginID = userSA.getUserID();
        long addressByID = address.getAddressByUser().getUserID();

        if (userLoginID == starID) {
            return false;
        }
        return userLoginID != addressByID;
    }

    Object onSuccess() {
        if (commentButton) {
            Address addressNew = new Address();
            addressNew.setAddressID(address.getAddressID());

            Comment commentLocal = new Comment();

            commentLocal.setCommentByUser(userSA);

            Text commentT = new Text();
            commentT.setText(commentText);

            commentLocal.setCommentText(commentT);
            ArrayList<Comment> commentList = new ArrayList<>();
            commentList.add(commentLocal);

            addressNew.setCommentList(commentList);

            int signal = myServices.commentAddressing(addressNew);
            if (signal == 10800) {
                setAddress(addressNew);
            }
        }
        return null;
    }

    Object onActivate() {
        if (!loginSA) {
            return Index.class;
        } else {
            return null;
        }
    }

    void onActivate(String systemMessage) {
        this.systemMessage = systemMessage;
    }

    String onPassivate() {
        return systemMessage;
    }
}
